package com.ryl.yomall.rabbitmq.service;

import com.alibaba.fastjson.JSONObject;
import com.ryl.yomall.rabbitmq.pojo.YomallCorrelationData;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.UUID;

@Service
public class RabbitService {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private RedisTemplate redisTemplate;


    /**
     *  发送消息
     * @param exchange 交换机
     * @param routingKey 路由键
     * @param message 消息
     */
    public void sendMessage(String exchange, String routingKey, Object message) {
        YomallCorrelationData correlationData=new YomallCorrelationData ();
        String uuid = UUID.randomUUID ( ).toString ( );
        correlationData.setId ( uuid );
        correlationData.setExchange ( exchange );
        correlationData.setRoutingKey ( routingKey );
        correlationData.setMessage ( message );
        //在缓存中保存一份message信息
        redisTemplate.opsForHash ().put ( "rabbit:message",correlationData.getId () ,JSONObject.toJSONString ( correlationData ));
        rabbitTemplate.convertAndSend(exchange, routingKey, message,correlationData);
    }

    /**
     * 延迟消息:基于死信
     */
    public void sendDeadLetterMessage(String exchange ,String routingKey ,Object msg,int delayTime) {
        YomallCorrelationData correlationData=new YomallCorrelationData ();
        String uuid = UUID.randomUUID ( ).toString ( );
        correlationData.setId ( uuid );
        correlationData.setExchange ( exchange );
        correlationData.setRoutingKey ( routingKey );
        correlationData.setMessage ( msg );
        correlationData.setDelay ( true );
        correlationData.setDelayTime ( delayTime );
        //在缓存中保存一份message信息
        redisTemplate.opsForHash ().put ( "rabbit:message",correlationData.getId () ,JSONObject.toJSONString ( correlationData ));
        rabbitTemplate.convertAndSend(exchange, routingKey, msg,
                message -> {
                     //延迟时间
                    // setExpiration : 死信过期转发       单位是毫秒    2*60*60
                    message.getMessageProperties ().setExpiration (String.valueOf ( delayTime*1000 ) );
                    System.out.println ("发送时间:"+ Calendar.getInstance ().getTime () );
                return message;
                },correlationData);


    }

    //基于延迟插件发送消息
    public void sendDelayMessage(String exchange ,String routingKey ,String msg ,int delayTime) {
        YomallCorrelationData correlationData=new YomallCorrelationData ();
        String uuid = UUID.randomUUID ( ).toString ( );
        correlationData.setId ( uuid );
        correlationData.setExchange ( exchange );
        correlationData.setRoutingKey ( routingKey );
        correlationData.setMessage ( msg );
        correlationData.setDelay ( true );
        correlationData.setDelayTime ( delayTime );
        //在缓存中保存一份message信息
        redisTemplate.opsForHash ().put ( "rabbit:message",correlationData.getId () ,JSONObject.toJSONString ( correlationData ));
        rabbitTemplate.convertAndSend(exchange, routingKey, msg,
                message -> {
                    //延迟时间
                    // setDelay : 延迟插件       单位是毫秒    2*60*60
                    message.getMessageProperties ().setDelay ( delayTime*1000  );

                    System.out.println ("发送时间:"+ Calendar.getInstance ().getTime () );
                    return message;
                },correlationData);
    }
}
